$(document).ready(function() {
	//获取项目名
	var contextPath = document.location.pathname; 
	var index =contextPath.substr(1).indexOf("/"); 
	contextPath = contextPath.substr(0,index+1); 
	
	var totalCount = $("#totalCount").val();
	var pSize = $("#pageSize").val();
	var pageIndex = $("#pageIndex").val();
	var pageCount = $("#pageCount").val();
	var gdeArr = new Array()// 定义一个数组,放礼品Id
	var orderArr = new Array()// 定义一个数组,放礼品Id
	
	/**
	 * 匿名评论的复选框的值的处理
	 * */
	$(".divLeft").delegate('#anonymous', 'click', function() {
		if($("#anonymous").attr('checked')=='checked'){
			$("#anonymous").removeAttr('checked');
			$("#anonymous").val('0');
		}else{
			$("#anonymous").attr('checked','checked');
			$("#anonymous").val('1')
		}
	});
	
	/**
	 * 清空数组
	 */
	function clearArrAll(){
		if(gdeArr.length>0){
			gdeArr.splice(0,gdeArr.length);
		}
		//获取gdeId的值
		var numgdeId = $("#gdeLen").val();
		
		for(i=0;i<numgdeId;i++){
			//礼品规格Id
			var gdeId = $("#gdeId_"+i).val();
			gdeArr.push(gdeId);
		}
	}
	
	/**
	 * 添加
	 * */
	$("#commBtn").delegate('#submitBtn', 'click', function() {
		var num = $("#num").val();
		for(i=0;i<num;i++){
			//礼品规格评论
			var contentDet = $("#contentDetAll_"+i+" div").html();
			$("#contentDet_"+i).val(contentDet);
			if(contentDet=="非常满意"){
				$("#contentDet_"+i).val("5");
			}
			if(contentDet=="满意"){
				$("#contentDet_"+i).val("4");
			}
			if(contentDet=="一般"){
				$("#contentDet_"+i).val("3");
			}
			if(contentDet=="不满意"){
				$("#contentDet_"+i).val("2");
			}
			if(contentDet=="很不满意"){
				$("#contentDet_"+i).val("1");
			}
			
			//物流评论
			var logistics = $("#logisticsAll div").html();
			if(logistics=="非常满意"){
				$("#logistics_"+i).val("5");
			}
			if(logistics=="满意"){
				$("#logistics_"+i).val("4");
			}
			if(logistics=="一般"){
				$("#logistics_"+i).val("3");
			}
			if(logistics=="不满意"){
				$("#logistics_"+i).val("2");
			}
			if(logistics=="很不满意"){
				$("#logistics_"+i).val("1");
			}
			
			//客服评论
			var servicerComm = $("#servicerCommAll div").html();
			if(servicerComm=="非常满意"){
				$("#servicerComm_"+i).val("5");
			}
			if(servicerComm=="满意"){
				$("#servicerComm_"+i).val("4");
			}
			if(servicerComm=="一般"){
				$("#servicerComm_"+i).val("3");
			}
			if(servicerComm=="不满意"){
				$("#servicerComm_"+i).val("2");
			}
			if(servicerComm=="很不满意"){
				$("#servicerComm_"+i).val("1");
			}
			//匿名评论
			var anonymous = $("#anonymous").val();
			$("#anonymous_"+i).val(anonymous);
		}
		$("#comments").attr("method","post");
		$("#comments").attr("action",contextPath+"/HRTWEB/commentPage/saveWeb.do");
		
	});
	
	/***-------------------demo1--------------***/
	var queryUrl = contextPath+"/HRTWEB/commentPage/findCommentByGdeId.do";
	queryComm(queryUrl);
	
	/***-------------------demo2--------------***/
	//下拉列表
	$("#commAllAdd").delegate('#selected', 'change', function() {
		$("#rankIdOption").val($(this).val());
		var commNum = $("#radioComment").val();
		commAndRank(commNum);//排序和单选的评论
		
	});
	/***-------------------demo3--------------***/
	//单选按钮
	$("#commAllAdd").delegate('[name="chiceComment"]', 'click', function() {
		clearArrAll();//清空数组再加载数据
		var commNum= $(this).val();
		$("#radioComment").val(commNum);
		commAndRank(commNum);//排序和单选的评论
	});
	
	/**-------------------demo4------------------**/
	function queryComm(queryUrl){
		//是否点击图片
		var heightNum = 0;
		
		if(heightNum==0){
			//高度自动
			if($(".active").attr("id")=="aaa"){
				var aa=$("#tabOne").height()+80;
				$("#tabHeightNum").val(aa);
				var a = $("#tabHeightNum").val();
				$(".swiper-slide").css("height",a);
				$(".swiper-container").css("height",a);
			}
			if($(".active").attr("id")=="bbb"){
				var bb=$("#tabTwo").height();
				$("#tabHeightNum").val(bb);
				var b = $("#tabHeightNum").val();
				$(".swiper-slide").css("height",b);
				$(".swiper-container").css("height",b);
			}
			if($(".active").attr("id")=="ccc"){
				var cc=$("#tabThree").height()+80;
				$("#tabHeightNum").val(cc);
				var c = $("#tabHeightNum").val();
				$(".swiper-slide").css("height",c);
				$(".swiper-container").css("height",c);
			}
		}
		
		//分页
		$('#pagination2').jqPaginator({
			totalCounts: totalCount,
			pageSize: pSize,
		    visiblePages: pageCount,
		    currentPage: 1,
		    prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
		    page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
		    next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
			onPageChange : function(num, type) {
				clearArrAll();//清空数组再加载数据
				//排序
				var rankIdOption = $("#rankIdOption").val();
//				alert("------"+rankIdOption);
				
				$("#pageIndex").val(num);
				
				$.ajax({
					url : queryUrl,
					dataType : 'json',
					data : {
						pageIndex :num,
						pageSize : pSize,
						ids:gdeArr.toString(),
						rankIdOption:rankIdOption
					},
					cache : false,
					success : function(data) {
						var results = data.pageResult.resultList;
						//礼品基本信息
						$("#commentList tr").remove();
						for (i = 0; i < results.length; i++) {
							//初评
							var commAll = results[i];
							//追评
							var addCommList = commAll.addComm;
							//初评的回复
							var replysAll = commAll.replys;
							//图片
							var picAll = commAll.picStrs;
							
							//替换匿名的用户名
							var mbr = commAll.hrtMemberName;
							if(mbr!=null){
								if(commAll.anonymous==1){
									var subUserName = mbr.substring(1,mbr.length-1);
									commAll.hrtMemberName = mbr.replace(subUserName,"***");
								}
							}else{
								commAll.hrtMemberName = "";
							}
							var rowContent = "";
							if(addCommList == null && addCommList.length==0){
								
								rowContent = rowContent + "<tr id='" 
									+ commAll.id
									+ "'> " 
									+ "<td colspan='2'> " 
									+ "<div style='float:left;margin-left:20px;' > " 
									+ "<div> " 
									+ commAll.content
									+ "</div>" 
								
								//多图片展示开始
									+"<div id='slider" 
									+ commAll.id
									+ "' class='slider'> <div class='minImgs' id='minImgs" 
									+ "'> <div class='min-box'>　<ul class='min-box-list' id='min-box-list" 
									+ "'> " ;
									//图片循环
								if(picAll!=null && picAll.length>0){
									for(f = 0; f < picAll.length; f++){
										var pics = picAll[f];
										rowContent = rowContent + "<li><div><img src='" 
										+ pics
										+ "'/></div></li>" ;
									}
								}
								rowContent = rowContent + "</ul> </div></div>" 
										+ "<div class='show-box' id='show-box" 
										//+ commAll.id
										+ "'> <ul>";
								//图片循环
								if(picAll!=null && picAll.length>0){
									for(f = 0; f < picAll.length; f++){
										var pics = picAll[f];
										rowContent = rowContent + "<li><a href=''><img src='" 
										+ pics
										+ "'/></a></li> " ;
									}
								}
								rowContent = rowContent + "</ul> " 
										+ "<div class='show-btn show-prev' id='show-prev" 
										+ "' ><span></span></div> " 
										+ "<div class='show-btn show-next' id='show-next" 
										+ "' ><span></span></div></div> " 
										+ "<div class='clear'></div></div>";
								
								//图片循环结束---------------------------
										
								rowContent = rowContent + "<font color='#999' >" 
									+ commAll.commTime 
									+ "</font> </div> ";
								if(replysAll!=null && replysAll.length!=0){
									for(f = 0; f < replysAll.length; f++){
										var replys = replysAll[f];
										//回复
										rowContent = rowContent + "<div style='clear:both;margin-left:20px;color: #963; font-size: 10px'> " 
										+ "解释：" 
										+ replys.content 
										+ "</div>";
									}
								}
								
								rowContent = rowContent + "</td> " 
									
									+ "<td style='width:20%;'> " 
									+ "<div> <div>颜色：</div> <div>尺码：</div> </div> " 
									+ "</td> "
									
									+ "<td style='width:15%;'>"
									+ commAll.hrtMemberName
									+ "(";
								
									if(commAll.anonymous==1){
										rowContent = rowContent + "匿名" ;
									}else{
										rowContent = rowContent + "公开" ;
									}
									
									rowContent = rowContent + ")" 
									+ "</td> " 
									+ "</tr>";
								$("#commentList").append(rowContent);
							}else{
								rowContent = rowContent +"<tr id='" 
									+ commAll.id
									+ "'> " 
									+ "<td colspan='2'> <div> " 
									+ "<div style='clear:both;margin-left:20px;'> " 
									+ "<div style='float:left;color:#999; '> " 
									+ "<div>初次评论:</div> " 
									+ commAll.commTime 
									+ " </div> <div style='float:left;' class='m-l-xx'>" 
									+ " <div>" 
									+ commAll.content 
									+ "</div> " ;
								//多图片展示
								if(picAll!=null && picAll.length>0){	
									rowContent = rowContent +"<div id='slider" 
									+ commAll.id 
									+ "' class='slider'> <div class='minImgs' id='minImgs" 
									+ "'> <div class='min-box'>　<ul class='min-box-list' id='min-box-list" 
									+ "'> " ;
									//图片循环
									for(f = 0; f < picAll.length; f++){
										var pics = picAll[f];
										rowContent = rowContent + "<li><div><img src='" 
										+ pics
										+ "'/></div></li>" ;
									}
								
								rowContent = rowContent + "</ul> </div></div>" 
										+ "<div class='show-box' id='show-box" 
										+ "'> <ul>";
								//图片循环
									for(f = 0; f < picAll.length; f++){
										var pics = picAll[f];
										rowContent = rowContent + "<li><a href=''><img src='" 
										+ pics
										+ "'/></a></li> " ;
									}
								rowContent = rowContent + "</ul> " 
										+ "<div class='show-btn show-prev' id='show-prev" 
										+ "' ><span></span></div> " 
										+ "<div class='show-btn show-next' id='show-next" 
										+ "' ><span></span></div></div> " 
										+ "<div class='clear'></div></div>"
									rowContent = rowContent + "</div> </div> " ;
								}
								//图片循环结束---------------------------
								
								if(replysAll !=null && replysAll.length != 0){
									for(f = 0; f < replysAll.length; f++){
										var replys = replysAll[f];
										//回复
										rowContent = rowContent + "<div style='clear:both;margin-left:20px;color: #963; font-size: 10px'> " 
										+ "解释：" 
										+ replys.content 
										+ "</div>";
									}
								}
								
									//追评
									for(j = 0; j < addCommList.length; j++){
										var addCommAll = addCommList[j];
										//追评的回复
										var addReplysAll = addCommAll.replys;
										
										//图片
										var addPicAll = addCommAll.picStrs;
										
										//替换匿名的用户名
										var mbrAdd = addCommAll.hrtMemberName;
										if(mbrAdd!=null){
											if(addCommAll.anonymous==1){
												var subUserName = mbrAdd.substring(1,mbrAdd.length-1);
												addCommAll.hrtMemberName = mbrAdd.replace(subUserName,"***");
											}
										}else{
											addCommAll.hrtMemberName = "";
										}
										//页面
										var rowContentAdd = "<div style='clear:both;margin-left:20px;'> " 
											+ "<div style='float:left;color:#999;'>  <div>" ;
										
										if(addCommAll.commTime=="0"){
											rowContentAdd = rowContentAdd + "收货当天";
										}else{
											rowContentAdd = rowContentAdd +"收货"
											+ addCommAll.commTime
											+ "天后";
										}
										rowContentAdd = rowContentAdd + "追加：" 
										+ "</div> </div> " 
										+ "<div style='float:left; ' class='m-l-xx'> " 
										+ "<div>" 
										+ addCommAll.content
										+ "</div> " ;
										//追评多图片展示
										if(addPicAll!=null && addPicAll.length>0){	
											rowContentAdd = rowContentAdd +"<div id='slider" 
											+ addCommAll.id 
											+ "' class='slider'> <div class='minImgs' id='minImgs" 
											+ "'> <div class='min-box'>　<ul class='min-box-list' id='min-box-list" 
											+ "'> " ;
											//图片循环
											for(f = 0; f < addPicAll.length; f++){
												var addPics = addPicAll[f];
												rowContentAdd = rowContentAdd + "<li><div><img src='" 
												+ addPics
												+ "'/></div></li>" ;
											}
										
											rowContentAdd = rowContentAdd + "</ul> </div></div>" 
												+ "<div class='show-box' id='show-box" 
												+ "'> <ul>";
											//图片循环
											for(f = 0; f < addPicAll.length; f++){
												var addPics = addPicAll[f];
												rowContentAdd = rowContentAdd + "<li><a href=''><img src='" 
												+ addPics
												+ "'/></a></li> " ;
											}
											rowContentAdd = rowContentAdd + "</ul> " 
												+ "<div class='show-btn show-prev' id='show-prev" 
												+ "' ><span></span></div> " 
												+ "<div class='show-btn show-next' id='show-next" 
												+ "' ><span></span></div></div> " 
												+ "<div class='clear'></div></div>"
											rowContentAdd = rowContentAdd + "</div> </div> " ;
										}
										//图片循环结束---------------------------
										
										
										rowContentAdd = rowContentAdd+ "</div> </div> " ;
										
										//回复
										if(addReplysAll !=null && addReplysAll.length!=0){
											for(h = 0; h<addReplysAll.length; h++){
												var addreplys = addReplysAll[h];
												rowContentAdd = rowContentAdd + "<div style='clear:both;margin-left:20px;color: #963; font-size: 10px'> " 
												+ "解释：" 
												+ addreplys.content
												+ "</div>";
											}
										}
										rowContent = rowContent+rowContentAdd;
									}
									
									/**------------------追评---------------------------*/
									
									rowContent = rowContent + "</div> " 
									+ "</td> " 
									+ "<td style='width:20%;'> <div> " 
									+ "<div>颜色：</div> " 
									+ "<div>尺码：</div> </div> " 
									+ "</td> " 
									+ "<td style='width:15%;'> " 
									+ commAll.hrtMemberName
									+ "(";
								
									if(commAll.anonymous==1){
										rowContent = rowContent + "匿名" ;
									}else{
										rowContent = rowContent + "公开" ;
									}
									
									rowContent = rowContent + ")</td> " 
									+ "</tr>";
									$("#commentList") .append(rowContent);
							}
						}
						
						//图片放大
						$("#commodity_div .slider").each(function(){
							
							/*alert("==list-item=>>"+$(".show-box li").css("display")=="list-item");
							alert("==block=>>"+$(".show-box li").css("display")=="block");*/
							
							var picSliderId = $(this).attr("id");
							var oSliderPic=$('#'+picSliderId),
							oShowBox=oSliderPic.find('#show-box'),
							oShowPrev=oSliderPic.find('#show-prev'),
							oShowNext=oSliderPic.find('#show-next'),
							oMinBoxList=oSliderPic.find('#min-box-list'),
							aShowBoxLi=oShowBox.find('li'),
							aMinBoxLi=oMinBoxList.find('li'),
							iMinLiWidth=aMinBoxLi.first().outerWidth(),
							iNum=aMinBoxLi.length
						    //判断总共有多少张图片，不够4张没有左右按钮		
							if(iNum>0 && iNum<4){
								if(iNum == 1){
									oShowPrev.css('display','none');
									oShowNext.css('display','none');
								}
							}
							
							//根据小图片的多少来计算出父容器的宽度,让其显示成一行
							oMinBoxList.css('width',iNum*iMinLiWidth+'px');

							//由于ie6 不支持max-width 所以设置图片的最大显示宽度 注意ie6下的css图片设置的宽度为100%,所以这里设置li即可
							var isIE6=!-[1,]&&!window.XMLHttpRequest;
							if(isIE6)  {
								for(var i =0;i<iNum;i++) {
									var o=aShowBoxLi.eq(i)
									o.css({width:o.outerWidth() > 643 ? 643 : o.outerWidth(),float:'none',margin:'0 auto'})
								}
							}

							oShowPrev.hover(function(){
								//采用链式操作减少dom操作.
								$(this).css({backgroundColor:'#F5F5F5'}).find('span').css({backgroundPosition:'-20px -47px'})
							},function(){
								$(this).css({backgroundColor:'#fff'}).find('span').css({backgroundPosition:'-20px 0px'})
							})
							oShowNext.hover(function(){
								$(this).css({backgroundColor:'#F5F5F5'}).find('span').css({backgroundPosition:'-89px -47px'})
							},function(){
								$(this).css({backgroundColor:'#fff'}).find('span').css({backgroundPosition:'-89px 0px'})
							})

							//键盘左右方向箭控制图片显示  (这里采用原生js)
							document.onkeyup=function(ev) {
								var o=window.event || ev; //获取事件对象
								if(o.keyCode==37) {
									changePrev()
								}
								else if(o.keyCode==39) {
									changeNext()
								}
							}

							//小图片列表添加点击事件
							aMinBoxLi.click(function(){
								var index=$(this).index();
								iNow=index;
								setLayout();//回调样式及动画函数
							})

							//左右按钮添加点击  回调
							oShowPrev.click(changePrev);
							oShowNext.click(changeNext);

							function changePrev() {   //iNow相当于下标索引，固下标索引不能小于0
								if(iNow<=0) {
									return;
								}
								iNow--;
								setLayout()
							}
							function changeNext() {
								if(iNow>=iNum-1) {
									return;
								}
								iNow++;
								setLayout();
							}

							//设置显示样式及动画
							function setLayout() {
								var oCurLi = aShowBoxLi.eq(iNow);
								aShowBoxLi.hide().eq(iNow).fadeIn('normal');
								oShowBox.css("display","block");
								aMinBoxLi.removeClass("cur").eq(iNow).addClass("cur");
								//限制图片运动距离，以免出现空白.
								if(iNow>=4 && iNow< iNum-4) {
									oMinBoxList.animate({left:-iMinLiWidth*(iNow-4)},100)
								}
								
								//高度自动display: list-item;
									heightNum = heightNum + 1;
									//现在的高度
									var picAA = $(this).height();
//									alert("--------*****picCC***6666**-------"+picAA);
									var heiNum = $("#tabHeightNum").val();
									var bb=$("#tabTwo").height()+picAA-heiNum-300;
									$(".swiper-slide").css("height",bb);
									$(".swiper-container").css("height",bb);
									$("#tabHeightImgNum").val(picAA);
							}
							
						});
							
					},
					error : function(html) {
						alert("gdeId comm----------------- Data failly");
						return;
					}
				});
			}
		});
	}
	/**-------------------demo5------------------**/
	function commAndRank(commNum){
		if(commNum=="1"){
			//分页
			var queryAllUrl = contextPath+"/HRTWEB/commentPage/findCommentByGdeId.do";
			queryComm(queryAllUrl)
		}
		if(commNum=="2"){
			var queryAddCommUrl = contextPath+"/HRTWEB/commentPage/getAddCommentByGdeId.do";
			//排序
			var rankIdOption = $("#rankIdOption").val();
			$.ajax({
				url : queryAddCommUrl,
				dataType : 'json',
				data : {
					pageIndex :1,
					pageSize : 10,
					ids:gdeArr.toString(),
					rankIdOption:rankIdOption
				},
				cache : false,
				success : function(data) {
					//初始化分页各值
					totalCount = data.pageResult.totalCount;
					pSize=data.pageResult.pageSize;
					pageCount=data.pageResult.pageCount;
					
					//分页
					queryComm(queryAddCommUrl)
				},
				error : function(html) {
					alert("gdeId Data failly");
					return;
				}
			});
			
		}
		if(commNum=="3"){
			var queryPicCommUrl = contextPath+"/HRTWEB/commentPage/getCommentPicByGdeId.do";
			//排序
			var rankIdOption = $("#rankIdOption").val();
			
			$.ajax({
				url : queryPicCommUrl,
				dataType : 'json',
				data : {
					pageIndex :1,
					pageSize : 10,
					ids:gdeArr.toString(),
					rankIdOption:rankIdOption
				},
				cache : false,
				success : function(data) {
					//初始化分页各值
					totalCount = data.pageResult.totalCount;
					pSize=data.pageResult.pageSize;
					pageCount=data.pageResult.pageCount;
					
					//分页
					queryComm(queryPicCommUrl)
				},
				error : function(html) {
					alert("gdeId Data failly");
					return;
				}
			});
		
		}
	}
	
	/**
	 * 刷新页面
	 * */
	function refresh() {
		window.location.reload();
	}
	
});

